import { Component, OnInit, OnDestroy } from '@angular/core';
import { AuthService } from 'src/app/shared/services/auth.service';
import { ActivatedRoute, Router } from '@angular/router';
import { LuckyTableService } from '../../shared/services/lucky-table.service';
import { AbstractLuckyDog, LuckyInfo } from '../../shared/models/lucky-dog';

@Component({
  selector: 'app-front',
  templateUrl: './front.component.html',
  styleUrls: ['./front.component.less']
})
export class FrontComponent implements OnInit, OnDestroy {
  excute = false;
  timer;
  visible = false;
  disabled = false;
  luckyDog: AbstractLuckyDog;
  randomData = '即将开始';
  constructor(
    public authService: AuthService,
    public luckyTableService: LuckyTableService,
    private route: ActivatedRoute,
    private router: Router
  ) {}
  onExcute() {
    this.excute = !this.excute;
    if (this.excute) {
      this.timer = setInterval(() => {
        this.randomData = this.luckyDog.show();
      }, 0);
    } else {
      clearInterval(this.timer);
      this.randomData = this.luckyDog.random();
      if (this.info.showCount === 0) {
        this.disabled = true;
        this.randomData = '暂无数据';
      }
    }
  }
  ngOnDestroy(): void {
    clearInterval(this.timer);
  }
  ngOnInit(): void {
    this.route.params.subscribe(data => {
      this.luckyDog = this.luckyTableService.get(data.id);
    });
  }
  open(): void {
    this.visible = true;
  }
  close(): void {
    this.visible = false;
  }
  reset() {
    this.luckyDog.reset();
    this.disabled = false;
    this.randomData = '即将开始';
  }
  back() {
    this.router.navigate(['/user']);
  }
  get info(): LuckyInfo {
    return this.luckyDog.info();
  }
}
